<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
          <a-collapse-panel key="1" header="基本信息">
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractNo">
                  <a-input
                    v-model="model.contractNo"
                    placeholder="请输入合同编号"
                    disabled
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="员工姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeId">
                  <select-person-input v-model="model.employeeId" disabled /></a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="员工编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employNo">
                  <a-input
                    v-model="model.employNo"
                    placeholder="请输入员工编号"
                    disabled
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所在单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workUnit">
                  <select-Company-input :disabled="formDisabled" v-model="model.workUnit" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所在部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departmentId">
                  <j-select-depart :disabled="formDisabled" v-model="model.departmentId"  @change="selectDepart"/>
                </a-form-model-item>
              </a-col>
              <!-- <a-col :span="8">
                <a-form-model-item label="所在部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departmentId">
                  <j-select-depart :disabled="formDisabled" v-model="model.departmentId" disabled />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teamId">
                  <select-team-input :disabled="formDisabled" v-model="model.teamId" :tdlx="3" />
                </a-form-model-item>
              </a-col> -->
              <a-col :span="8">
                <a-form-model-item label="岗位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionId">
                  <j-dict-select-tag
                    type="list"
                    v-model="model.positionId"
                    dictCode="sys_position,name,id,del_flag = 0 and audit_status=2" 
                    placeholder="请选择岗位名称"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractType">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.contractType"
                    dictCode="employee_constract"
                    placeholder="请选择合同类型"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="薪资（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="salary">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    v-model="model.salary"
                    placeholder="请输入薪资"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="实习薪资"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="practicalSalary"
                >
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    v-model="model.practicalSalary"
                    placeholder="请输入实习薪资"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="合同开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginDate">
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择合同开始日期"
                    v-model="model.beginDate"
                    style="width: 100%"
                    @change="selectRange"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="合同期限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractRange">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.contractRange"
                    dictCode="contract_range"
                    placeholder="请选择合同期限"
                    @input="selectRange"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="合同到期日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
                  <j-date
                    placeholder="请选择合同到期日期"
                    v-model="model.endDate"
                    style="width: 100%"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="合同状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractStatus">
                  <j-dict-select-tag
                    type="list"
                    v-model="model.contractStatus"
                    dictCode="contract_status"
                    placeholder="请选择合同状态"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
                  <a-textarea :disabled="formDisabled" v-model="model.remark" rows="4" placeholder="请输入备注" :max-length="500"/>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="2" header="附件信息">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
                  <j-upload :disabled="formDisabled" v-model="model.attachment"></j-upload>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
        </a-collapse>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import moment from 'moment'
import SelectPersonInput from '@/views/scy/components/SelectPersonInput'
import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'

export default {
  name: 'ErpHrEmployeeContractForm',
  components: {
    SelectPersonInput,
    SelectTeamInput,
    SelectCompanyInput
  },
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      activeKey:['1','2'],
      model: {
      },
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        contractType: [
          { required: true, message: '请选择合同类型!' }
        ],
        workUnit: [
          { required: true, message: '请选择所在单位!' }
        ],
        departmentId: [
          { required: true, message: '请选择所在部门!' }
        ],
        positionId: [
          { required: true, message: '您选择的部门下未找到岗位!' }
        ],
        salary: [
          { required: true, pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的薪资!' }
        ],
        beginDate: [
          { required: true, message: '请选择合同开始时间!' }
        ],
        contractRange: [
          { required: true, message: '请选择合同期限!' }
        ],
      },
      url: {
        queryPostRecordByInfo:'/api/erp/files/employee/erpEmployeePostChangeRecord/queryPostRecordByInfo',
        getBillNo:"/api/erp/hr/contract/erpHrEmployeeContract/getBillNo",
        add: '/api/erp/hr/contract/erpHrEmployeeContract/add',
        edit: '/api/erp/hr/contract/erpHrEmployeeContract/edit',
        queryById: '/api/erp/hr/contract/erpHrEmployeeContract/queryById'
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    //选择部门
    selectDepart(data){
      if(data){
        getAction(this.url.queryPostRecordByInfo,{ employeeId: this.model.employeeId,departId:this.model.departmentId }).then(res => {
          if(res.success){
            let data=res.result;
            if(data){
              this.$set(this.model,'positionId',data[0].postId);
            }
          }
        })
      }else{
        this.$set(this.model,'positionId',null);
      }
      
    },
    //获取编号
    getBillNo(){
      let that=this;
      getAction(this.url.getBillNo, {}).then((res) => {
        if (res.success) {
          let data = res.result.records || res.result
          this.$set(this.model, 'contractNo', data);
        }
      })
    },
    selectRange(){
      if(this.model.contractRange){
        //存在开始时间
        if(this.model.beginDate){
          let endDate=moment(this.model.beginDate).add(this.model.contractRange, 'years').subtract(1,'days').format('YYYY-MM-DD');
            this.model.endDate=endDate;
          if(endDate>moment().format('YYYY-MM-DD')){
            this.model.contractStatus='1';
          }else{
            this.model.contractStatus='3';
          }
        }
      }
    },
    add(record) {
      this.$set(this.modelDefault,'employeeId',record.id);
      this.$set(this.modelDefault,'employNo',record.employeeNo);
      // this.$set(this.modelDefault,'workUnit',record.orgDepart);
      // this.$set(this.modelDefault,'departmentId',record.departmentId);
      // this.$set(this.modelDefault,'teamId',record.teamId);
      //this.$set(this.modelDefault,'positionId',record.mainPost);
      this.edit(this.modelDefault)
      this.getBillNo();
    },
    edit(record) {
      this.$refs.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
